﻿<UserControl 
    x:Class="myManga.Views.LibraryView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:models="clr-namespace:myManga.Models"
    xmlns:ui="clr-namespace:myManga.UI"
    xmlns:bakabox="clr-namespace:System.Windows.Controls;assembly=BakaBox"
    SnapsToDevicePixels="True" 
    TextElement.FontFamily="Segoe UI">
    <UserControl.Resources>
        <ContextMenu x:Key="libContextMenu">
            <MenuItem Header="Delete" Command="{Binding DeleteLibraryItem}"/>
            <MenuItem Header="Bookmark"/>
        </ContextMenu>
    </UserControl.Resources>
    <Grid Background="#252525">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListView
            HorizontalContentAlignment="Stretch"
            Background="#252525"
            BorderThickness="1,0,0,0"
            BorderBrush="#555"
            SelectedItem="{Binding Path=CurrentMangaItem, Mode=TwoWay, IsAsync=True}"
            ItemsSource="{Binding Path=MangaItems, IsAsync=True}"
            SelectionMode="Single" Grid.Column="1"
            ScrollViewer.PanningMode="VerticalOnly"
            ItemContainerStyle="{StaticResource LibraryListViewItem}"
            Style="{StaticResource BalancedWrapListView}"/>
        <Expander
            IsExpanded="{Binding Path=DetailsOpen, Mode=TwoWay, IsAsync=True}"
            Background="#252525"
            BorderThickness="0"
            BorderBrush="#111"
            Foreground="White"
            FontSize="16"
            ExpandDirection="Right"
            x:Name="InfoExpander">
            <Expander.Header>
                <TextBlock Margin="1,5"  Text="{Binding Path=CurrentMangaItem.Name, FallbackValue=Un-named}">
                    <TextBlock.LayoutTransform>
                        <RotateTransform Angle="270"/>
                    </TextBlock.LayoutTransform>
                </TextBlock>
            </Expander.Header>
            <Border
                DataContext="{Binding Path=CurrentInfo, IsAsync=True}"
                Background="#111"
                TextElement.FontSize="12"
                Grid.Column="1"
                Grid.Row="1"
                Width="336">
                <Grid TextBlock.Foreground="White">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="18"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <ListBox
                        x:Name="ChapterList"
                        Background="#111"
                        Grid.Row="2"
                        ItemsSource="{Binding Path=ChapterEntries, IsAsync=True}" 
                        HorizontalContentAlignment="Stretch"
                        BorderThickness="0"
                        AlternationCount="2"
                        VirtualizingStackPanel.VirtualizationMode="Recycling"
                        ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                        <ListBox.Resources>
                            <Style TargetType="{x:Type ListBoxItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                            <Border x:Name="_Item" Margin="0,1" Padding="2" Background="#0000">
                                                <Grid TextBlock.Foreground="White">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition/>
                                                        <ColumnDefinition Width="Auto"/>
                                                    </Grid.ColumnDefinitions>
                                                    <StackPanel>
                                                        <TextBlock TextWrapping="NoWrap" TextTrimming="CharacterEllipsis">
                                                            <TextBlock.Text>
                                                                <MultiBinding StringFormat="{}v{0} c{1}.{2}">
                                                                    <Binding Path="Volume"/>
                                                                    <Binding Path="Chapter"/>
                                                                    <Binding Path="SubChapter"/>
                                                                </MultiBinding>
                                                            </TextBlock.Text>
                                                        </TextBlock>
                                                        <TextBlock x:Name="TitleName" Padding="10,0,0,0" FontSize="18" Text="{Binding Path=Name, FallbackValue=Un-Named}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
                                                    </StackPanel>
                                                    <Button 
                                                    Grid.Column="1"
                                                    Height="24"
                                                    Style="{StaticResource GlowBtn}"
                                                    Command="{Binding Path=DataContext.ReadChapter, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                                                    CommandParameter="{Binding}">
                                                        <StackPanel>
                                                            <Image Source="/myManga;component/Resources/Images/Metro/White/No%20Circle/Layout.png" Margin="-10" Stretch="Uniform" />
                                                            <TextBlock VerticalAlignment="Center" Text="Read"/>
                                                        </StackPanel>
                                                    </Button>
                                                </Grid>
                                            </Border>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                                    <Setter TargetName="_Item" Property="Background" Value="#333"/>
                                                </Trigger>

                                                <Trigger Property="IsSelected" Value="True">
                                                    <Setter TargetName="_Item" Property="Background" Value="#555"/>
                                                </Trigger>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter TargetName="_Item" Property="Background" Value="#999"/>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ListBox.Resources>
                    </ListBox>

                    <Grid
                        Grid.Row="1">
                        <TextBlock Text="Chapters:" FontSize="19" Padding="3"/>
                        <StackPanel
                            HorizontalAlignment="Right" 
                            VerticalAlignment="Center">
                            <Button 
                                Content="Download Remaining" 
                                FontSize="13"
                                Command="{Binding Path=DataContext.DownloadRemainingChapters, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}}"/>
                            <Button 
                                Content="Download All" 
                                FontSize="13"
                                Command="{Binding Path=DataContext.DownloadAllChapters, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}}"/>
                        </StackPanel>
                    </Grid>
                    <StackPanel>
                        <TextBlock Margin="3,1" TextWrapping="Wrap">
                        <Bold Foreground="LightGray">Alt. Title:</Bold> <Run Text="{Binding Path=AltTitle, FallbackValue=Unknown, Mode=OneWay}"/>
                        </TextBlock>
                        <TextBlock Margin="3,1" TextWrapping="Wrap">
                        <Bold Foreground="LightGray">Released:</Bold> <Run Text="{Binding Path=Released, FallbackValue=Unknown, Mode=OneWay}"/>
                        </TextBlock>
                        <TextBlock Margin="3,1" TextWrapping="Wrap">
                        <Bold Foreground="LightGray">Status:</Bold> <Run Text="{Binding Path=Status, FallbackValue=Unknown, Mode=OneWay}"/>
                        </TextBlock>
                        <TextBlock Margin="3,1" TextWrapping="Wrap">
                        <Bold Foreground="LightGray">Author:</Bold> <Run Text="{Binding Path=Author, FallbackValue=Unknown, Mode=OneWay}"/>
                        </TextBlock>
                        <TextBlock Margin="3,1" TextWrapping="Wrap">
                        <Bold Foreground="LightGray">Artist:</Bold> <Run Text="{Binding Path=Artist, FallbackValue=Unknown, Mode=OneWay}"/>
                        </TextBlock>
                        <TextBlock Margin="3,1" TextWrapping="Wrap">
                        <Bold Foreground="LightGray">Genre:</Bold> <Run Text="{Binding Path=Genre, FallbackValue=Unknown, Mode=OneWay}"/>
                        </TextBlock>
                        <TextBlock Margin="3,1" TextWrapping="Wrap">
                        <Bold Foreground="LightGray">Manga Site:</Bold> <Run Text="{Binding Path=Site, FallbackValue=Unknown, Mode=OneWay}"/>
                        </TextBlock>
                    </StackPanel>

                    <ToggleButton
                        Grid.Column="1" 
                        Grid.RowSpan="3" 
                        IsChecked="{Binding ElementName=InfoExpander, Path=IsExpanded, Mode=TwoWay, IsAsync=True}"
                        BorderThickness="0">
                        <ToggleButton.Background>
                            <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                                <GradientStop Color="#111" Offset="0.5" />
                                <GradientStop Color="#555" Offset="1" />
                            </LinearGradientBrush>
                        </ToggleButton.Background>
                    </ToggleButton>
                    
                    <Grid Grid.Row="3">
                        <StackPanel Orientation="Horizontal">
                            <Button Command="{Binding Path=DataContext.DeleteLibraryItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}}" Content="Delete"/>
                            <Button Command="{Binding Path=DataContext.BookmarkLibraryItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}}" Content="Bookmark"/>
                        </StackPanel>
                    </Grid>
                </Grid>
            </Border>
        </Expander>
        <Button 
            HorizontalAlignment="Left" 
            VerticalAlignment="Bottom" 
            SnapsToDevicePixels="True"
            Margin="1,3"
            Command="{Binding Path=UpdateLibrary}">
            <Button.LayoutTransform>
                <RotateTransform Angle="270"/>
            </Button.LayoutTransform>
            <StackPanel Orientation="Horizontal">
                <Image 
                    Source="/myManga;component/Resources/Images/Metro/Black/No Circle/refresh.png" 
                    Stretch="Uniform" 
                    Height="21" 
                    Margin="-2"/>
                <TextBlock Text="{DynamicResource ResourceKey=RefreshButtonText}" VerticalAlignment="Center"/>
            </StackPanel>
        </Button>
    </Grid>
</UserControl>
